#ifndef SEQIOT_CONSTANT_H
#define SEQIOT_CONSTANT_H

//只有超过2010-01-01的才算有效时间,否则认为时钟不正确
#define TIME_20100101 1262275200

/**协议类型*/
typedef enum {
    cp_gpio = 20,
    cp_modbus_rtu = 1,
    cp_modbus_tcp = 2,
    cp_dlt645_97 = 11,
    cp_dlt645_07 = 12,
    cp_iec101 = 31,
    cp_iec103 = 33,
    cp_iec104 = 34
} protocol_e;

_Bool protocol_is_modbus(protocol_e type);

_Bool protocol_is_dlt645(protocol_e type);

/**通道类型*/
typedef enum {
    ct_serial = 10, ct_rs232 = 11, ct_rs485 = 12, ct_tcpip = 1, ct_lora = 2, ct_zigbee = 3
} channel_type_e;

_Bool channel_is_serial(channel_type_e type);

/**数据段，和modbus的寄存器类型相似*/
typedef enum {
    seg_do = 1, //数字量输出
    seg_di = 2, //数字量输入
    seg_ao = 3, //模拟量输出
    seg_ai = 4, //模拟量输入
    seg_pr = 5, //参变量
    seg_fn = 6, //计算量
    seg_max = 7
} segment_e;

/**数据类型*/
typedef enum {
    dt_bit = 1,
    dt_byte = 2,
    //2bytes
    dt_short = 3,
    dt_neg_short = 4,
    dt_word = 5,
    //3bytes
    dt_mid = 6,
    dt_neg_mid = 7,
    dt_tword = 8,
    //4bytes
    dt_integer = 9,
    dt_neg_int = 10,
    dt_dword = 11,
    //8bytes
    dt_long = 12,
    dt_neg_long = 13,
    dt_qword = 14,

    dt_float = 15,
    dt_double = 16,
    dt_bcd_integer = 21,
    dt_bcd_decimal = 22,
    dt_bcd_unsigned_integer = 23,
    dt_bcd_unsigned_decimal = 24,
} data_type_e;

data_type_e get_data_type(const char *str);

int get_data_len(data_type_e dt);

/**时间格式*/
typedef enum {
    tt_none = 80, tt_ymd = 81, tt_y4md = 82, tt_hns = 83, tt_ymdhns = 84, tt_y4mdhns = 85
} time_type_e;

time_type_e get_time_type(const char *str);

typedef enum {
    read_only = 0, write_only = 1, read_write = 2
} writable_e;

writable_e get_writable(const char *str);

typedef enum {
    dc_none = 0, //
    dc_bit = -1, //开关变化
    dc_soe = -2, //告警标志
    dc_event = -3, //告警值
    dc_analog = 1, //电流/电压
    dc_power = 2, //功率
    dc_energy = 3, //电量
    dc_demand = 4, //需量
    dc_harmonic = 5, //谐波统计
    dc_harmonic_odd = 6, //奇次谐波
    dc_harmonic_even = 7, //偶次谐波
    dc_param = 8, //常用参数
    dc_param_ex = 9, //不常用参数
    dc_hourly = 10, //时段数据
    dc_daily = 11, //日数据
} data_catalog_e;

data_catalog_e get_data_catalog(const char *str);

typedef enum {
    vt_nvl = 0, //
    vt_point = 1, //单点遥信
    vt_dpoint = 2, //双点遥信
    vt_position = 3, //步进
    vt_bitstr = 4, //32位整数
    vt_normalized = 5, //标度化值 y = x / pow(10, decimals)
    vt_scaled = 6, //归一化值 y = x * scale + adjust
    vt_floating = 7, //短浮点数
    vt_totals = 8 //抄表数
} value_type_e;

typedef enum {
    cy_startup = -4, //程序启动时报送
    cy_connect = -3, //设备连接后报送
    cy_changed = -2, //数据变化时报送
    cy_non_zero = -1, //数据为非0时报送
    pt0s = 0, //不报送
    pt1s = 1,
    pt5s = 5,
    pt10s = 10,
    pt20s = 20,
    pt30s = 30,
    pt1m = 60,
    pt5m = pt1m * 5,
    pt15m = pt1m * 15,
    pt30m = pt1m * 30,
    pt1h = pt1m * 60,
    pt2h = pt1h * 2,
    pt3h = pt1h * 3,
    pt4h = pt1h * 4,
    pt6h = pt1h * 6,
    p1d = pt1h * 24,
    p1w = p1d * 7,
    p1m = p1d * 30,
    p1y = p1m * 12
} period_e;
typedef enum {
    up_iec104,
    up_modbus,
    up_bacnet,
    up_opcua,
    up_mqtt,
    up_sgcc,
    up_aliyun,
    up_huawei,
    up_baidu,
} upload_type_e;
typedef enum {
    pf_shell = 0,
    pf_packet = 1,
    pf_scada = 2,
    pf_json_kv = 3,
    pf_json_cmd = 10,
    pf_json_conf = 90,
    pf_upgrade_gz = 91,
    pf_json_sysconf = 92
} payload_format_e;

char *replace_metric(char *metric, size_t size);

#endif //SEQIOT_CONSTANT_H
